// $_FILEHEADER_BEGIN ***************************
// 版权声明:北京畅聊天下科技有限公司版权所有
// Copyright © 2012 - Changliao Technology Co.Ltd. All Rights Reserved
// 文件名称: BarrageItem.m
// 创建日期: 16/1/24
// 创 建 人: 甄鑫
// 文件说明: <#content#>
// $_FILEHEADER_END ******************************


#import "BarrageItem.h"
#import "UIView+UIView_ITAdditions.h"

@implementation BarrageItemModel
- (NSURL *)imageUrl{
    return [NSURL URLWithString:[HEADERIMAGE_FETCH stringByAppendingString:self.uid]];
}
-(void)setContentStr:(NSString *)contentStr{
    _contentStr = contentStr;
}

-(NSAttributedString *)content{
    return [[NSAttributedString alloc]initWithString:_contentStr attributes:nil];
}

- (Class)viewClass{
    Class classTmp = NSClassFromString(@"BarrageItemView");
    return classTmp;
}
@end


#define kItemViewDefaultW (100)
#define kItemViewDefaultH (20)

#define kItemImageViewW (15)
#define kItemImageViewH (15)
#define kItemLabelFont ([UIFont systemFontOfSize:12])

@implementation BarrageItemView : UIView
{
    UIImageView * _bgImageView;
    UIImageView * _headImageView;
    UILabel     * _contentLabel;
}

+ (BarrageItemView *)create{
    BarrageItemView * itemView = [[BarrageItemView alloc]initWithFrame:CGRectMake(0, 0, kItemViewDefaultW, kItemViewDefaultH)];
    [itemView setBackgroundColor:[UIColor clearColor]];
    
    itemView->_bgImageView = [[UIImageView alloc]init];
    [itemView addSubview:itemView->_bgImageView];
    
    itemView->_contentLabel = [[UILabel alloc]init];
    [itemView addSubview:itemView->_contentLabel];
    
    itemView->_headImageView = [[UIImageView alloc]init];
    [itemView addSubview:itemView->_headImageView];
    
    return itemView;
}

- (void)setItemModel:(BarrageItemModel *)itemModel{
    _itemModel = itemModel;
    
    [_headImageView setImageWithURL:[itemModel imageUrl]];
    [_headImageView setFrame:CGRectMake(3, 0, kItemImageViewW, kItemImageViewW)];
    _headImageView.layer.cornerRadius = _headImageView.frame.size.height/2;
    _headImageView.clipsToBounds = YES;
    [_headImageView setCenterY:self.center.y];

    
    NSString * contentStr = [[itemModel content]string];
#pragma mark -----qiweipeng
    
    CGFloat textW = [contentStr sizeWithFont:kItemLabelFont constrainedToSize:CGSizeMake(100, self.height)].width;
#pragma mark---------------
    [_contentLabel setText:contentStr];
    [_contentLabel setTextColor:[UIColor whiteColor]];
    [_contentLabel setFont:kItemLabelFont];
    [_contentLabel setFrame:CGRectMake([_headImageView getFrame_right]+5, 0, textW, self.height)];
    
    [_bgImageView setSize:CGSizeMake([_contentLabel getFrame_right] + 5, self.height)];
    _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    [_bgImageView setImage:[UIImage imageNamed:@"danmuBlack"]];
    _bgImageView.clipsToBounds = YES;
    _bgImageView.layer.cornerRadius = _bgImageView.frame.size.height/2.0;
    [self setFrame_width:[_bgImageView getFrame_right]];

    if (itemModel.isShowRedSpecial) {
        // 创建特殊的红色背景的弹幕视图
        UIView * redSpecialView = [BarrageItemRedSepecialView createWithContent:itemModel.redSpecialContent];
        [redSpecialView setFrame:CGRectMake(0, [_bgImageView getFrame_Bottom] + 5, redSpecialView.width, redSpecialView.height)];
        [self addSubview:redSpecialView];
        [self setFrame_height:[redSpecialView getFrame_Bottom]];
        [self setFrame_width:[redSpecialView getFrame_right]];
    }
}
@end

#define kSpecialItemViewDefaultW (132)
#define kSpecialViewDefaultH (20)

@implementation BarrageItemRedSepecialView : UIView{
    UIImageView * _bgImageView;
    UILabel * _mainLabel;
}
+ (BarrageItemRedSepecialView *)createWithContent:(NSString *)content{
    BarrageItemRedSepecialView * itemView = [[BarrageItemRedSepecialView alloc]initWithFrame:CGRectMake(0, 0, kSpecialItemViewDefaultW, kSpecialViewDefaultH)];
    [itemView setBackgroundColor:[UIColor clearColor]];
    
    itemView->_bgImageView = [[UIImageView alloc]init];
    [itemView->_bgImageView setImage:[UIImage imageNamed:@"danmuRed"]];
    [itemView->_bgImageView setSize:itemView.size];
    [itemView addSubview:itemView->_bgImageView];
    
    itemView->_mainLabel = [[UILabel alloc]init];
    [itemView->_mainLabel setFont:kItemLabelFont];
    [itemView->_mainLabel setSize:itemView.size];
    [itemView->_mainLabel setText:content];
    [itemView->_mainLabel setTextAlignment:NSTextAlignmentCenter];
    [itemView->_mainLabel setTextColor:[UIColor whiteColor]];
    [itemView addSubview:itemView->_mainLabel];
    
    return itemView;

}
@end